#ifndef _CROSSLIST_BASE_H_
#define _CROSSLIST_BASE_H_

#include <stdint.h>
#include <stdbool.h>

//十字链表
class CrossList
{
public:
    void *value;

    CrossList(void *val = NULL);
    virtual ~CrossList();
    CrossList(const CrossList&) = delete;

    //当前节点和 node 的关系
    bool IsInsideOf(CrossList *node);
    bool IsChildOf(CrossList *node);
    bool IsParentOf(CrossList *node);

    //添加节点,返回index,-1失败
    int32_t PushBack(CrossList *node);
    int32_t PushFront(CrossList *node);
    int32_t Insert(CrossList *node, int32_t index);

    int32_t PushBackChild(CrossList *node);
    int32_t PushFrontChild(CrossList *node);
    int32_t InsertChild(CrossList *node, int32_t index);

    //弹出节点
    CrossList* PopBackChild();
    CrossList* PopFrontChild();

    //从链表中移除自己(保留child)
    void Remove();

    //查找
    CrossList* Parent();
    CrossList* Prev();
    CrossList* Next();
    CrossList* FirstChild();
    CrossList* LastChild();
    CrossList* At(int32_t index);
    CrossList* ChildAt(int32_t index);

    //基本信息
    int32_t Index();
    int32_t Child();

    //互斥锁(按需要实现)
    virtual void Lock();
    virtual void UnLock();

private:
    int32_t index = 0; //在当前横向链表中的序号,从0数起
    CrossList *prev = NULL;
    CrossList *next = NULL;

    CrossList *parent = NULL;

    int32_t child = 0; //子链表节点个数
    CrossList *firstChild = NULL; //指向子链表头
    CrossList *lastChild = NULL; //指向子链表尾
};

#endif // _CROSSLIST_BASE_H_
